package com.zretc.support.service.serviceimpl;

import com.zretc.support.dao.QuestionDao;
import com.zretc.support.dao.ReplyDao;
import com.zretc.support.entity.Question;
import com.zretc.support.service.QuestionSerive;
import com.zretc.support.tool.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/***
 * 问题实现类
 */
@Service
public class QuestionServiceImpl implements QuestionSerive {

    @Autowired
    private QuestionDao qd;

    @Autowired
    private ReplyDao replyDao;

    @Autowired
    private PageData pageData;

    Logger log = LoggerFactory.getLogger(QuestionServiceImpl.class);

    /**
     * 问题模糊查询加分页
     *
     * @param questionTitle   问题标题
     * @param questionContent 问题内容
     * @param startTime       开始时间
     * @param endTime         结束时间
     * @param page            页码
     * @param size            每页的数据量
     * @return
     */
    @Override
    public PageData<Question> getQuestionByLAP(String questionTitle, String questionContent, Date startTime, Date endTime, int page, int size) {
        //分页设置
        Pageable pageable = PageRequest.of(page, size);
        //调用方法
        Page<Question> page1 = qd.getQuestionByLAP(questionTitle, questionContent, startTime, endTime, pageable);
        //实例化对象用于
        PageData<Question> pd = new PageData<>();
        //给pageDate赋值
        pd.setCurrent(page1.getNumber());
        pd.setRecords(page1.getContent());
        pd.setTotal((int) page1.getTotalElements());
        pd.setSize(page1.getSize());
        pd.setCode(200);
        pd.setMsg("查询成功！");
        //返回数据
        return pd;
    }

    @Override
    public Question addQuestion(Question question) {
        question.setQuestionStatus(1);
        return qd.saveAndFlush(question);
    }

    @Override
    @Transactional
    public boolean deleteQuestion(Long questionId) {
        Question question = new Question();
        question.setQuestionId((long) questionId);
        try {
            long n1 = qd.count();

            qd.delete(question);
            long n2 = qd.count();
            if (n1 - n2 == 0) {
                throw new NullPointerException();
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean updateQuestion(Question question) {
        Question q = qd.saveAndFlush(question);
        if (q != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Question getQuestionById(long questionId) {
        Question question = qd.findQuestionByQuestionId(questionId);
        return question;
    }


    /**
     * 查询问题标题列表
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageData<Question> getQuestionTitleList(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Question> pd = qd.getQuestionTitleList(pageable);
        for (int i = 0; i < pd.getContent().size(); i++) {
            pd.getContent().get(i).setHotQuestion((long)replyDao.countRepliesByQuestion_QuestionId(pd.getContent().get(i).getQuestionId()));
        }
        pageData.setCurrent(pd.getNumber());
        pageData.setRecords(pd.getContent());
        pageData.setTotal((int) pd.getTotalElements());
        pageData.setSize(pd.getSize());
        pageData.setCode(200);
        pageData.setMsg("查询成功！");
        return pageData;
    }
}
